package com.hzit.sb2.service.impl;

import com.hzit.sb2.bean.weixinEntity.BaseMsg;
import com.hzit.sb2.bean.weixinEntity.event.ClickPullMsgEvent;
import com.hzit.sb2.bean.weixinEntity.text.TextMsg;
import com.hzit.sb2.service.IAutoCallBackService;
import com.hzit.sb2.util.MyConst;
import com.hzit.sb2.util.baidu.BaiduToken;
import com.hzit.sb2.util.weixin.Dom4jUtil;
import com.thoughtworks.xstream.XStream;
import org.dom4j.Document;
import org.dom4j.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Random;

@Service
public class AutoCallBackServiceImpl implements IAutoCallBackService {
    @Autowired
    Dom4jUtil dom4jUtil;
    @Autowired
    BaiduToken baiduToken;

    public String cut = "✌";//剪刀
    public String close = "✋";//布
    public String rock = "\uD83E\uDD1C";//石头


    @Override
    public String autoCallBackMsg(Document document) {
//<xml><ToUserName><![CDATA[gh_3a65e6f47db1]]></ToUserName>
//<FromUserName><![CDATA[opH4ExKJvtMzxqis2ho_7E3TRYyQ]]></FromUserName>
//<CreateTime>1596695070</CreateTime>
//<MsgType><![CDATA[text]]></MsgType>
//<Content><![CDATA[hh]]></Content>
//<MsgId>22859175126177822</MsgId>
//</xml>
        XStream xStream = new XStream();
        xStream.alias("xml", TextMsg.class);
        String toUserName = dom4jUtil.getValue(document, "ToUserName");
        String fromUserName = dom4jUtil.getValue(document, "FromUserName");
        String msgType = dom4jUtil.getValue(document, "MsgType");
        String xml = document.asXML();
//        TextMsg fromXML = (TextMsg) xStream.fromXML(xml);
        if("text".equals(msgType)){
            TextMsg textMsg = new TextMsg();
            textMsg.setToUserName(fromUserName);
            textMsg.setFromUserName(toUserName);
            textMsg.setMsgType(MyConst.TEXT_MSG_TYPE);
            textMsg.setCreateTime(new Date().getTime());
            textMsg.setContent("\uD83D\uDD1E");
            String sendXml = xStream.toXML(textMsg);
            return sendXml;
        }else  if("event".equals(msgType)){
            //subscribe
            String event = dom4jUtil.getValue(document, "Event");
            if("subscribe".equals(event)){
                TextMsg textMsg = new TextMsg();
                textMsg.setToUserName(fromUserName);
                textMsg.setFromUserName(toUserName);
                textMsg.setMsgType(MyConst.TEXT_MSG_TYPE);
                textMsg.setCreateTime(new Date().getTime());
                textMsg.setContent("欢迎关注本订阅号，更多资讯爆料尽在这里\uD83D\uDE00");
                String sendXml = xStream.toXML(textMsg);
                return sendXml;
            }
            TextMsg textMsg = new TextMsg();
            int random = (int)(Math.random()*3);
          //  String[] strArr = {"石头","剪刀","布"};
            String eventKey = dom4jUtil.getValue(document, "EventKey");
            if("rock".equals(eventKey)){
                switch (random)
                {
                    case 1 :
                       textMsg.setContent("你出的是【"+rock+"】,系统出的是【"+rock+"】,平局");
                        break; //可选
                    case 2 :
                        textMsg.setContent("你出的是【"+rock+"】,系统出的是【"+cut+"】,你赢了");
                        break; //可选
                    //你可以有任意数量的case语句
                    default : //可选
                        textMsg.setContent("你出的是【"+rock+"】,系统出的是【"+close+"】,你输了");
                }
            }else if("cut".equals(eventKey)){
                switch (random)
                {
                    case 1 :
                        textMsg.setContent("你出的是【"+cut+"】,系统出的是【"+rock+"】,你输了");
                        break; //可选
                    case 2 :
                        textMsg.setContent("你出的是【"+cut+"】,系统出的是【"+cut+"】,平局");
                        break; //可选
                    //你可以有任意数量的case语句
                    default : //可选
                        textMsg.setContent("你出的是【"+cut+"】,系统出的是【"+close+"】,你赢了");
                }
            }else {
                switch (random)
                {
                    case 1 :
                        textMsg.setContent("你出的是【"+close+"】,系统出的是【"+rock+"】,你赢了");
                        break; //可选
                    case 2 :
                        textMsg.setContent("你出的是【"+close+"】,系统出的是【"+cut+"】,你输了");
                        break; //可选
                    //你可以有任意数量的case语句
                    default : //可选
                        textMsg.setContent("你出的是【"+close+"】,系统出的是【"+close+"】,平局");
                }
            }
            textMsg.setToUserName(fromUserName);
            textMsg.setFromUserName(toUserName);
            textMsg.setMsgType(MyConst.TEXT_MSG_TYPE);
            textMsg.setCreateTime(new Date().getTime());
            String sendXml = xStream.toXML(textMsg);
            return sendXml;
        }else  if("image".equals(msgType)){
            String picUrl = dom4jUtil.getValue(document, "PicUrl");
            TextMsg textMsg = new TextMsg();
            textMsg.setToUserName(fromUserName);
            textMsg.setFromUserName(toUserName);
            textMsg.setMsgType(MyConst.TEXT_MSG_TYPE);
            textMsg.setCreateTime(new Date().getTime());
            String words = baiduToken.getWords(picUrl);
            textMsg.setContent(words);
            String sendXml = xStream.toXML(textMsg);
            return sendXml;
        }
        else {
            TextMsg textMsg = new TextMsg();
            textMsg.setToUserName(fromUserName);
            textMsg.setFromUserName(toUserName);
            textMsg.setMsgType(MyConst.TEXT_MSG_TYPE);
            textMsg.setCreateTime(new Date().getTime());
            textMsg.setContent("暂只支持文本类型,其他类型开发中");
            String sendXml = xStream.toXML(textMsg);
            return sendXml;
        }
    }
}
